<?php

declare(strict_types=1);

namespace app\api\controller;

use think\facade\View;
use think\response\Json;
use app\common\model\Cart as CartModel;
use app\common\model\User;
use app\common\model\SpecItem;
use app\common\model\Seckill;
use app\common\model\SeckillProduct;
use app\common\model\ProductSpecValue;

use app\common\tool\Result;

class Cart extends Base
{
    //购物车列表
    public function index()
    {
        $map[] = ['uid', '=', $this->user->id];
        $map[] = ['is_del', '=', 0];
        $lists = CartModel::where($map)->select();
        foreach ($lists as $k => $v) {
            /**
             * 判断购物车秒杀商品是否过期
             */
            if ($v->shoptype == 'seckill') {
                if ($v->seckill_id == 0) {
                    $v->force()->delete($v->id);
                    // CartModel::where(['id'=>$v['id']])->delete();
                }
                $Seckill = Seckill::where(['id' => $v['seckill_id'], 'is_del' => 0, 'status' => 1])->find();
                if (empty($Seckill)) {
                    $v->force()->delete($v->id);
                    // CartModel::where(['id'=>$v['id']])->delete();
                } else {
                    /**
                     * 判断秒杀活动里的商品是否存在
                     */
                    $SeckillProduct = SeckillProduct::where(['seckill_id' => $v['seckill_id']])->column('product_id');
                    if (time() > $Seckill['end_time'] || !in_array($v['pid'], $SeckillProduct)) {

                        $v->force()->delete($v->id);
                        // CartModel::where(['id'=>$v['id']])->delete();
                    }
                }
            }
        }
        $list = CartModel::getList($map, false, 'update_time', ['product'], 'id,pid,price,num,spce,shoptype');
        // $param = $this->request->param();
        // $list = CartModel::getPageList($map, 'id,pid,price,num,spce,shoptype', $param['page'], 20, false, ['product']);
        foreach ($list as $k => $v) {
            $spce = explode('-', $v->spce);
            $spceinfo = ProductSpecValue::where(['sku_id' => $v->spce, 'pid' => $v->pid])->find();
            if (empty($spceinfo)) {
                $v->is_del = 1;
                $v->save();
                unset($list[$k]);
            }
            $Specname = SpecItem::whereIn('id', $spce)->column('name');
            $v->title = $v->product->title;
            $v->stock = $spceinfo->stock;
            if ($spceinfo->icon == '') {
                $v->icon = $v->product->icon;
            } else {
                $v->icon = $spceinfo->icon;
            }
            $v->Specname = implode('-', $Specname);
            $v->check = 0;
            unset($v->product);
        }
        return Result::success('获取数据成功！', $list);
    }
    //获取购物车数量
    public function getCartNum()
    {

        $token = $this->request->header('token');
        if (!empty($token)) {
            $user = User::where(['token' => $token, 'is_del' => 0])->find();
            if (!empty($user)) {
                $map = ['uid' => $user->id];
                $count = CartModel::where($map)->count();
            } else {
                $count = 0;
            }
        } else {
            $count = 0;
        }
        return Result::success('获取数据成功！', $count);
    }
    //获取购物车商品数量
    public function getCartCount()
    {
        $count = CartModel::where(['uid' => $this->user->id])->count();
        return Result::success('获取数据成功！', $count);
    }
    //添加购物车
    public function add()
    {
        $param = $this->request->post();
        if (!$param['num'] || $param['num'] <= 0) {
            return Result::fail('数量错误');
        }
        return Result::fastReturn(CartModel::add($this->user, $param));
    }
    //购物车操作
    public function edit()
    {
        $param = $this->request->post();
        return Result::fastReturn(CartModel::edit($param['type'], $param['id'], $param['num'], $this->user->id));
    }
    //清空购物车
    public function emptyCart()
    {
        $param = $this->request->post();
        if (!$param['cart_id']) {
            return Result::fail('请选择要删除的商品');
        }
        $cart_id = explode(',', $param['cart_id']);
        CartModel::where('uid', $this->user->id)->whereIn('id', $cart_id)->delete();
        return Result::success('已删除');
    }
    //购物车结算
    public function submit()
    {
        $param = $this->request->post();
        if (!$param['cart_id']) {
            return Result::fail('请选择要结算的商品');
        }
        $param['cart_id'] = explode(',', $param['cart_id']);
        return Result::fastReturn(CartModel::submit($param, $this->user));
    }
    //修改购物里商品数量
    public function cartSaveNum()
    {
        $param = $this->request->post();
        if (!$param['id']) {
            return Result::fail('请选择修改的商品');
        }
        return Result::fastReturn(CartModel::cartSaveNum($param, $this->user));
    }
}
